//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//------------------------------------------------------------------------------
using System;
using System.IO;
using System.Web.UI.WebControls;
using System.Xml;
using DNAide.Core;

namespace DNAide.Web
{
	/// <summary>
	/// Wrapper for helping with site map writing
	/// </summary>
	public class DNAideSiteMapWriter : IDisposable
	{
		public static XmlWriterSettings DefaultSettings
		{
			get { return _settings; }
		}

		private static readonly XmlWriterSettings _settings;
		private readonly XmlWriter _writer;
		private readonly bool _enableLocalisation;

		static DNAideSiteMapWriter()
		{
			_settings = new XmlWriterSettings();
			_settings.Indent = true;
			_settings.IndentChars = "\t";
			_settings.ConformanceLevel = ConformanceLevel.Document;
		}

		public DNAideSiteMapWriter(string outputFileName, bool enableLocalisation)
		{
			string directory = Path.GetDirectoryName(outputFileName);

			if (!Directory.Exists(directory))
				Directory.CreateDirectory(directory);

			_writer = XmlWriter.Create(outputFileName, _settings);
			_enableLocalisation = enableLocalisation;
		}

		public void StartSiteMap()
		{
			_writer.WriteStartElement("siteMap", "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0");

			if (_enableLocalisation)
				_writer.WriteAttributeString("enableLocalization", "true");
		}

		public void EndSiteMap()
		{
			EndSiteMap(false);
		}

		public void EndSiteMap(bool flush)
		{
			_writer.WriteEndElement();

			if (flush)
				Flush();
		}


		public void WriteFullNode(string key, string url, string title)
		{
			WriteStartNode(key, url, title, string.Empty);
			WriteEndNode();
		}


		public void WriteFullNode(string key, string url, string title, string rewriteToUrl)
		{
			WriteStartNode(key, url, title, rewriteToUrl);
			WriteEndNode();
		}


		public void WriteFullNode(string key, string url, string title, string rewriteToUrl, DNAideSiteMapAttribute[] attributes)
		{
			WriteStartNode(key, url, title, rewriteToUrl, attributes);
			WriteEndNode();
		}

		public void WriteStartNode(string key, string url, string title)
		{
			WriteStartNode(key, url, title, string.Empty, null);
		}

		public void WriteStartNode(string key, string url, string title, string rewriteToUrl)
		{
			WriteStartNode(key, url, title, rewriteToUrl, null);
		}

		public void WriteStartNode(string key, string url, string title, string rewriteToUrl, DNAideSiteMapAttribute[] attributes)
		{
			_writer.WriteStartElement("siteMapNode");
			_writer.WriteAttributeString("key", key);
			_writer.WriteAttributeString("url", url);
			_writer.WriteAttributeString("title", title);
			_writer.WriteAttributeString("rewriteToUrl", rewriteToUrl);

			if (attributes != null)
			{
				foreach (DNAideSiteMapAttribute attr in attributes)
				{
					if (attr == null || string.IsNullOrEmpty(attr.Name) || attr.Value == null)
						continue;

					_writer.WriteAttributeString(attr.Name, StringAide.FormatInvariant("{0}", attr.Value));
				}
			}
		}

		public void WriteEndNode()
		{
			_writer.WriteEndElement();
		}

		public void Close()
		{
			_writer.Close();
		}

		public void Flush()
		{
			_writer.Flush();
		}

		#region IDisposable Members

		public void Dispose()
		{
			Dispose(true);
		}

		private void Dispose(bool disposing)
		{
			if (disposing)
				Close();
		}

		#endregion
	}
}
